home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRNOMY / SFS101S.ZIP / RO101.ZIP / RO_WORD.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  10KB  |  415 lines

  1. /********************************************************/
  2. /*                            */
  3. /*    ro_word.c    word-handling routines for ro    */
  4. /*                            */
  5. /*    ro version 1.10                    */
  6. /*                            */
  7. /*    Portions copyright (c) 1989 by Ted A. Campbell    */
  8. /*        Bywater Software            */
  9. /*        P. O. Box 4023                */
  10. /*        Duke Station                */
  11. /*        Durham, NC  27706            */
  12. /*                            */
  13. /*    Contains portions of ROFF4, Version 1.60    */
  14. /*      (c) 1983, 4 by Ernest E. Bergmann               */
  15. /*        Physics, Building #16            */
  16. /*        Lehigh University            */
  17. /*        Bethlehem, Pa. 18015            */
  18. /*                            */
  19. /*    Contains portions of ROFF4, Version 1.61    */
  20. /*      (c) 1985 by Konrad Kwok                         */
  21. /*        20 3rd Street, Section M        */
  22. /*        Fariview Park,                */
  23. /*        Hong Kong                */
  24. /*                            */
  25. /*    ro and its predecessor ROFF4 are based on     */
  26. /*    the ROFF text processor described in Kernigan    */
  27. /*    and Plauger's now-classic text <Software Tools> */
  28. /*                            */
  29. /* Permission is hereby granted for all commercial and    */
  30. /* non-commercial reproduction and distribution of this */
  31. /* material provided this notice is included.        */
  32. /*                            */
  33. /********************************************************/
  34.  
  35. #include "ro.h"
  36.  
  37. /****************************************/
  38.  
  39. puttl3( s1, s2, s3, pageno )
  40.    char *s1, *s2, *s3;
  41.    int pageno;          /*put out three part title, none
  42.                containing '\n', with
  43.                optional page numbering; aligning
  44.                with page margins,0 & ro_rmval[0];*/
  45.    {
  46.    int size1, gap1, size2, gap2, size3, gaps, remain;
  47.    size1 = strln3( s1, FALSE, pageno );
  48.    ro_outtop = ro_ltop;
  49.    ro_outbot = ro_lbot;
  50.    size2 = strln3(s2,FALSE,pageno);
  51.    if (ro_ltop<ro_outtop)
  52.       ro_outtop=ro_ltop;
  53.    size3 = strln3(s3,FALSE,pageno);
  54.    if (ro_ltop<ro_outtop)
  55.       ro_outtop=ro_ltop;
  56.    gaps = ro_max( 0, ro_rmval[0]-size1-size2-size3);
  57.    gap1 = gaps/2;
  58.    gap2 = gaps-gap1;
  59.    remain = ro_rmval[0];
  60.    if ( size1 <= remain )
  61.       {
  62.       puttl( s1, pageno);
  63.       remain -= size1;
  64.       }
  65.    if ( gap1 < remain )
  66.       {
  67.       blanks( gap1 );
  68.       remain -= gap1;
  69.       }
  70.    if ( size2 <= remain )
  71.       {
  72.       puttl( s2, pageno );
  73.       remain -= size2;
  74.       }
  75.    if ( gap2 < remain )
  76.       {
  77.       blanks( gap2 );
  78.       remain -= gap2;
  79.       }
  80.    if ( size3 <= remain )
  81.       {
  82.       puttl( s3, pageno );
  83.       remain -= size3;
  84.       }
  85.    printout();
  86.    ro_outc('\r');
  87.    }
  88.  
  89. /****************************************/
  90.  
  91. blanks( i )
  92.    int i;   /*sends i blanks to ro_out2buf*/
  93.    {      
  94.    if(i<0) return;
  95.    for( ; i ; i--) putout( BLANK );
  96.    }
  97.  
  98. /****************************************/
  99. /* Gets from source line three part title that it
  100.    transfers to buffer delineated by ttl1 which has
  101.    capacity for all three strings; none of the strings
  102.    will contain '\n' */
  103.  
  104. gettl3( sl, ttl1, ttl2, ttl3 )
  105.    char *sl, *ttl1, *ttl2, *ttl3;
  106.    {
  107.    char c, *dp;
  108.  
  109. #ifdef   DEBUG
  110.    if ( ro_debug == TRUE ) 
  111.       {
  112.       fprintf( stderr, "DEBUG:  gettl3() sl = <%s> \n", sl );
  113.       }
  114. #endif
  115.  
  116.    /* pass over command */
  117.  
  118.    for ( c = *sl; c != ' ' && c != '\n' && c != '\t'; sl++ )
  119.       {
  120.       c = *sl;
  121.       }
  122.  
  123.    /*advance to first non-blank or '\n' */
  124.  
  125.    for ( ; c == ' ' || c == '\t'; sl++ )
  126.       {
  127.       c = *sl;
  128.       }
  129.  
  130.    /* c should now be set to the delimiter */
  131.    /* advance beyond delimiter, if present */
  132.  
  133.    if ( c != '\n' && !c ) 
  134.       {
  135.       sl++;
  136.       }
  137.  
  138.    dp = ttl1;
  139.    transfer( &sl, &dp, c );
  140.    dp = ttl2;
  141.    transfer( &sl, &dp, c);
  142.    dp = ttl3;
  143.    transfer( &sl, &dp, c);
  144.  
  145. #ifdef   DEBUG
  146.    if ( ro_debug == TRUE )
  147.       {
  148.       fprintf( stderr,"DEBUG:  delim=<%c>\nT1=<%s>\nT2=<%s>\nT3=<%s>;\n",
  149.          c, ttl1, ttl2, ttl3);
  150.       }
  151. #endif
  152.  
  153.    }
  154.  
  155. /****************************************/
  156. /* Copy string from source to destination.   Original delim.
  157. can be \0, \n, or char.   The pointer to the source is updated
  158. to point at the \0, \n, or past char.   In destination, delim.
  159. is always replaced by \0.   The destination pointer always
  160. points past this \0. */
  161.  
  162. transfer( src, dst, c )
  163.    char c;           /* terminal character */
  164.    char **src;      /* source string */
  165.    char **dst;      /* destination string */
  166.    {
  167.    char a;
  168.  
  169.    a = **src;
  170.    while ( a != c && a != '\n' && a != '\0' && a )
  171.       {
  172.       **dst = a;   
  173.       (*dst)++;
  174.       (*src)++;   
  175.       a = **src;
  176.       }
  177.    **dst = '\0';
  178.    (*dst)++;
  179.    if ( a != '\n' ) 
  180.       {
  181.       (*src)++;
  182.       }
  183.    }
  184.  
  185. /**********************************************************
  186.  centers a line of text
  187. **********************************************************/
  188.  
  189. center (line)
  190.    char *line;
  191.    {
  192.    ro_tival = ro_max(( ro_rmval[0]+ro_tival-strln3(line,FALSE,1))/2, 0 );
  193.    ro_outtop=ro_ltop;
  194.    ro_outbot=ro_lbot;
  195.    return;
  196.    }
  197.  
  198. /************************************************************
  199. Revised April 24,83.Transfers next word from in to out.   Scans
  200. off leading white space from in.   If there is no word, returns
  201. FALSE.   Otherwise, input is truncated on left of word which is
  202. transfered to out without leading or trailling blanks.
  203. WE_HAVE_A_WORD will be returned.   If the transfered word
  204. terminates a sentence then ro_sentence is set to 1, otherwise it
  205. is reset to FALSE.
  206. **************************************************************/
  207.  
  208. int getwrd (in,   out )
  209.    char *in, *out;
  210.    {
  211.    char *pin, *pout, c, cm, cp;
  212.  
  213.    skip_blanks(in);
  214.    replace_char(in,TAB,BLANK);
  215.    pin = in;
  216.    pout = out;
  217.    c = *pin;
  218.    ro_sentence=FALSE;
  219.    if(c==NEWLINE || c=='\0')
  220.       {
  221.       *out='\0';
  222. #ifdef   DEBUG
  223.       if ( ro_debug == TRUE ) 
  224.          {
  225.          fprintf( stderr, "DEBUG:  getwrd() = <%s> \n", out );
  226.          }
  227. #endif
  228.       return(FALSE);
  229.       }
  230.    while(c!=BLANK && c!=NEWLINE && c)
  231.       {
  232.       *(pout++) = c;
  233.       *pin = BLANK;
  234.       c=*(++pin);
  235.       }
  236.    *pout = '\0';   /*terminate out string*/
  237.    cm=*(pout-1);
  238.    cp=*(pin+1);
  239.    switch (cm)
  240.       {
  241.    case ':' :
  242.    case ';' :
  243.    case '?' :
  244.    case '!' :
  245.          ro_sentence=1;
  246.          break;
  247.    case '.' :
  248.          if(cp==BLANK||cp==NEWLINE||c==NEWLINE)
  249.             {
  250.             ro_sentence=1;
  251.             }
  252.       }
  253. #ifdef   DEBUG
  254.    if ( ro_debug == TRUE ) 
  255.       {
  256.       fprintf( stderr, "DEBUG:  getwrd() = <%s> \n", out );
  257.       }
  258. #endif
  259.    return(WE_HAVE_A_WORD);
  260.    }
  261.  
  262. /*******************************************************
  263. Truncates white-space characters at the end of a string.
  264. ********************************************************/
  265.  
  266. trunc_bl (string)
  267.    char *string;
  268.    {
  269.    char *ptr;
  270.    int k;
  271.    k = strlen (string);
  272.    ptr = &string[ k-1 ];    /* char before terminating nul */
  273.    while (*ptr==BLANK || *ptr==TAB || *ptr==NEWLINE)      
  274.          *ptr--   = '\0';
  275.    }
  276.  
  277. /*********************************************
  278.        distribute words evenly across a line
  279. **********************************************/
  280.  
  281. spread ( line, nextra, no_words)
  282.    char *line;
  283.    int nextra;   /* no. extra places left in line */
  284.    int no_words;    /* no. words in the line      */
  285.    {
  286.    int i, j, nblanks, nholes;
  287.  
  288. #ifdef   DEBUG
  289.    if ( ro_debug == TRUE ) 
  290.       {
  291.       fprintf( stderr, "DEBUG:  spread(): line = <%s>,\n        nextra = %d, no_words = %d\n",
  292.          line, nextra, no_words   );
  293.       }
  294. #endif
  295.  
  296.    if (nextra <= 0 || no_words <= 1)
  297.       return;
  298.    ro_dir = !(ro_dir);
  299.    nholes = no_words - 1;
  300.    trunc_bl (line);
  301.    i = strlen(line) - 1 ; /* last character of string */
  302.    j = ro_min(MAXLINE-2,i+nextra);   /* last   position in output */
  303.    line[j+1] = '\0';
  304.    for ( ; i<j ; i--, j-- )
  305.       { 
  306.          line[j] = line[i];
  307.          if ( line[i] == BLANK)
  308.             { 
  309.              if (ro_dir == 0) nblanks=(nextra-1)/nholes+1;
  310.              else         nblanks = nextra/nholes;
  311.              nextra = nextra - nblanks;
  312.              nholes = nholes - 1;
  313.              for ( ; nblanks > 0;   nblanks-- )
  314.                line[--j] = BLANK;
  315.             }
  316.       }
  317.    }
  318.  
  319. /************************************************
  320. place portion of title line with optional page no. in ro_out2buf
  321. *************************************************/
  322.  
  323. puttl ( str, num )
  324.    char *str;
  325.    int num;
  326.    {
  327.    char c;
  328.  
  329.    for ( ; c = *str; str++ )
  330.       {
  331.          if ( c != NUMSIGN ) putout(c);
  332.          else putnum(num);
  333.       }
  334.    }
  335.  
  336. /*******************************************
  337.  put out num to ro_out2buf (conversion)
  338. ********************************************/
  339. putnum ( num )
  340.    int num;
  341.    {
  342.    int i, nd;
  343.    char chars[10];
  344.    nd = itoc ( num, chars, 10 );
  345.    for ( i=0;i<nd; i++) putout(chars[i]);
  346.    }
  347.  
  348. /************************************************
  349.  convert int num to char string in numstr
  350. *************************************************/
  351.  
  352. itoc ( num, numstr, size )
  353.    int num;
  354.    char *numstr;
  355.    int size;      /* largest size of numstr */
  356.    {
  357.    int absnum, i, j, k, d;
  358.    absnum = abs (num);
  359.    numstr[0] = '\0';
  360.    i = 0;
  361.    do       { 
  362.          i++;
  363.          d = absnum % 10;
  364.          numstr[i] = d + '0';
  365.          absnum = absnum/10;
  366.       }
  367.    while ( absnum != 0 && i<size );
  368.    if ( num < 0 && i<size )
  369.       { 
  370.          i++;
  371.          numstr[i] = '-';
  372.       }
  373.    for( j=0; j<i; j++ )
  374.       { 
  375.          k = numstr[i];
  376.          numstr[i] = numstr[j];
  377.          numstr[j] = k;
  378.          i--;
  379.       }
  380.    return ( strlen(numstr) );
  381.    }
  382.  
  383. /***************************************
  384.  
  385.    putout()        places c in ro_out2buf[]
  386.  
  387. ***************************************/
  388.  
  389. putout( c )
  390.    char c;
  391.    {
  392.    if ( c == ro_scval[0] )
  393.       {
  394.       c = BLANK;
  395.       }
  396.    if ( c == NEWLINE )
  397.       {
  398.       c = '\0';
  399.       }
  400.    ro_out2buf[ ro_bpos++ ] = c;
  401.    ro_out2buf[ ro_bpos ] = '\0';           /*safty net*/
  402.    }
  403.  
  404. /************************************
  405.  replace c1 in string with c2
  406. *************************************/
  407.  
  408. replace_char (string, c1, c2)
  409.    char *string, c1, c2;
  410.    {
  411.    int i;
  412.    for (i=0; string[i]; i++)
  413.          if (string[i] == c1)   string[i] = c2;
  414.    }
  415.